package com.example.mall.web.controller;

import com.example.mall.domain.UserRole;
import com.example.mall.dto.RoleDto;
import com.example.mall.dto.UserRoleDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserRoleQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    @Autowired
    public IUserRoleService userRoleService;

    /**
     * 分页查询登录角色表信息
     * @param userRoleQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询登录角色表信息",notes = "分页查询登录角色表信息",httpMethod = "GET")
    public JSONResult queryPage(UserRoleQuery userRoleQuery){
        PageDTO<UserRoleDto> userRoleDtoPageDTO = userRoleService.queryPage(userRoleQuery);
        return JSONResult.success("查询成功",userRoleDtoPageDTO);
    }

    /**
     * 根据id查询登录角色表信息
     * @param userRoleId
     * @return
     */
    @GetMapping("query/{userRoleId}")
    @ApiOperation(value = "根据id查询登录角色表信息",notes = "根据id查询登录角色表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("userRoleId")Long userRoleId){
        //1.根据id查询用户信息
        if (userRoleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERROLEID_NOT_ID);
        }
        UserRole userRole = userRoleService.getById(userRoleId);
        return JSONResult.success("查询成功",userRole);
    }

    /**
     * 根据id查询登录角色表信息
     * @param userRoleId
     * @return
     */
    @GetMapping("queryUserRoleById/{userRoleId}")
    @ApiOperation(value = "根据id查询登录角色表信息",notes = "根据id查询登录角色表信息",httpMethod = "GET")
    public JSONResult queryUserRoleById(@PathVariable("userRoleId")Long userRoleId){
        //1.根据id查询用户信息
        if (userRoleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USERROLEID_NOT_ID);
        }
        UserRole userRole = userRoleService.queryUserRoleById(userRoleId);
        return JSONResult.success("查询成功",userRole);
    }

    /**
     * 查询全部登录角色表信息
     * @param
     * @return
     */
    @GetMapping("queryUserRoleList")
    @ApiOperation(value = "查询全部登录角色表信息",notes = "查询全部登录角色表信息",httpMethod = "GET")
    public JSONResult queryUserRoleList(){
        List<UserRole> roles = userRoleService.queryUserRoleList();
        return JSONResult.success("查询成功",roles);
    }

    /**
     * 新增登录角色表
     * @param userRoleDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增登录角色表",notes = "新增登录角色表",httpMethod = "POST")
        public JSONResult insert(@RequestBody UserRoleDto userRoleDto){
        userRoleService.insert(userRoleDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改登录角色表信息
     * @param userRoleDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改登录角色表信息",notes = "修改登录角色表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserRoleDto userRoleDto){
        userRoleService.update(userRoleDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除登录角色表信息
     * @param userRoleId
     * @return
     */
    @PostMapping("delete/{roleId}")
    @ApiOperation(value = "删除登录角色表信息",notes = "删除登录角色表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("roleId") Long userRoleId) {
        userRoleService.delete(userRoleId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除登录角色表信息
     * @param userRoleIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除登录角色表信息",notes = "批量删除登录角色表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> userRoleIds) {
        userRoleService.deleteBatch(userRoleIds);
        return JSONResult.success("批量删除成功");
    }
}
